package homework;

public class p1654 implements Runnable {
    public static int count=10;  //初始库存量

    public p1654(){
        MakerThread makerThread1=new MakerThread(this,"工人一号");
        MakerThread makerThread2=new MakerThread(this,"工人二号");
        CustomerThread customerThread1=new CustomerThread(this,"顾客一号");
        CustomerThread customerThread2=new CustomerThread(this,"顾客二号");

        makerThread1.start();
        makerThread2.start();
        customerThread1.start();
        customerThread2.start();
    }

    public static void main(String[] args){
        new p1654();
    }

    @Override
    public void run() {
        Thread thread =Thread.currentThread();

        try {
            if (thread.getName().matches("工人\\S\\S")) {
                    synchronized (this) {
                        p1654.count++;
                        System.out.println(thread.getName()+" 生产一个商品 商品总数：" + count);
                        notifyAll();
                        wait();
                    }
            }else{
                synchronized (this){
                    p1654.count--;
                    System.out.println(thread.getName()+" 购买一个商品 商品总数：" + count);
                    notifyAll();
                    wait();
                }

            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class MakerThread extends Thread{

    public MakerThread(Runnable r,String name){
        super(r,name);
    }

    public void MakeProduct(){
            p1654.count++;
            System.out.println(this.getName()+"制造了一个商品  ALL:"+p1654.count);

    }


}

class CustomerThread extends Thread{

    public CustomerThread(Runnable r,String name){
        super(r,name);
    }

    public void PurchaseProduct(){
        p1654.count--;
        System.out.println(this.getName()+"购买了一个商品  Count:"+p1654.count);
    }

}




